# Maintainer: Kevin Mihelich <kevin@archlinuxarm.org>
# Contributor: Mike Brown <mbrown@archlinuxarm.org>

pkgname=wiringpi
_pkgname=WiringPi
pkgver=181.1ad03dd
pkgrel=1
pkgdesc='GPIO Interface library for the Raspberry Pi'
arch=('armv7h' 'aarch64')
makedepends=('git')
license=('LGPL3')
url='https://github.com/WiringPi/WiringPi.git'
source=("git+https://github.com/WiringPi/WiringPi.git")
md5sums=('SKIP')

pkgver() {
  cd ${_pkgname}
  echo $(git rev-list --count master).$(git rev-parse --short master)
}

prepare() {
  cd ${_pkgname}
  sed -i 's|/usr/local/bin/gpio|/usr/bin/gpio|' wiringPi/wiringPi.c
  VERSION=$(cat VERSION)
}

build() {
  cd ${_pkgname}

  make -C wiringPi all
  make -C devLib all INCLUDE+="-I${srcdir}/${_pkgname}/wiringPi"

  ln -s "${srcdir}/${_pkgname}/wiringPi/libwiringPi.so.${VERSION}" wiringPi/libwiringPi.so
  ln -s "${srcdir}/${_pkgname}/devLib/libwiringPiDev.so.${VERSION}" devLib/libwiringPiDev.so

  make -C gpio INCLUDE+="-I${srcdir}/${_pkgname}/wiringPi -I${srcdir}/${_pkgname}/devLib" LDFLAGS+="-L${srcdir}/${_pkgname}/wiringPi -L${srcdir}/${_pkgname}/devLib"
  make -C wiringPiD INCLUDE+="-I${srcdir}/${_pkgname}/wiringPi -I${srcdir}/${_pkgname}/devLib" LDFLAGS+="-L${srcdir}/${_pkgname}/wiringPi -L${srcdir}/${_pkgname}/devLib"
}

package(){
  cd ${_pkgname}

  make LDCONFIG= PREFIX= DESTDIR="${pkgdir}/usr" -C wiringPi install
  make LDCONFIG= PREFIX= DESTDIR="${pkgdir}/usr" -C devLib install
  install -d "${pkgdir}/usr/bin"
  make PREFIX= DESTDIR="${pkgdir}/usr" WIRINGPI_SUID=1 -C gpio install
  install -m0755 wiringPiD/wiringpid "${pkgdir}/usr/bin"
  install -Dm644 COPYING.LESSER "$pkgdir/usr/share/licenses/$pkgname/COPYING.LESSER"
}
